﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HOBA.SMSSender
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.Count > 0 && Request.QueryString["s"] != "")
            {
                lblMessage.Visible = true;
                if (Request.QueryString["s"] == "0")
                    lblMessage.Text = "Wrong username or password! Please try again.";
            }
            else
            {
                lblMessage.Visible = false;
                lblMessage.Text = "";
            }
        }

        protected void Submit(object sender, EventArgs e)
        {
            if (CheckLogin(txtUsername.Text, txtPassword.Text) == true)
            {
                Session.Add("username", txtUsername.Text);
                Session.Add("password", txtPassword.Text);
                Response.Redirect("Admin.aspx");
            }
            else
            {
                Response.Redirect("index.aspx?s=0");
            }
        }

        private bool CheckLogin(string username, string password)
        {
            try
            {
                MySoapService service = new MySoapService("http://center.fibosms.com/Service.asmx");
                string response = service.CheckClient(txtUsername.Text, txtPassword.Text);
                return FileUtility.ReadCheckLogin(response);
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
